package com.bicon.botu.mall.component.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.mall.component.entity.Dictionary;
import com.bicon.botu.mall.component.dto.DictionaryDTO;
import com.bicon.botu.mall.component.service.IComponentDictionaryService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.BeanMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 字典接口
 * </p>
 *
 * @author tandk
 * @since 2017-07-31
 */
@RestController
@RequestMapping("/V1.0.0/api/base/dictionary")
public class DictionaryApi extends BaseController {
    @Autowired
    IComponentDictionaryService componentDictionaryService;

    /**
     * 从redis中查询字典
     *
     * @param id id
     */
    @RequestMapping(value = "/findDictionaryFromRedis")
    public BaseResult findDictionaryFromRedis(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return sendResult400();
        }
        return sendResult200(componentDictionaryService.findDictionaryFromRedis(id));
    }


    /**
     * 从redis中查询省、市、区
     *
     * @param code     查询（省、市、区）对应的code
     * @param parentId 父地区id
     */
    @RequestMapping(value = "/findAreaFromRedis")
    public BaseResult findAreaFromRedis(@RequestParam String code, @RequestParam int parentId) {
        return componentDictionaryService.findAreaFromRedis(code, parentId);
    }

    /**
     * 根据父id查询所有node结构子字典（比如：省、市、区）
     *
     * @param parentId 父id
     * @param code     code
     */
    @RequestMapping(value = "/findAllDictionaryNode")
    public BaseResult findAllDictionaryNode(Integer parentId, String code) {
        return sendResult200(componentDictionaryService.findAllDictionaryNode(parentId, code));
    }

    /**
     * 根据父code查询子字典集合
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/findListByParentCode")
    public BaseResult findListByParentCode(String code) {
        List<Dictionary> list = componentDictionaryService.findListByParentCode(code);
        if (list != null && !list.isEmpty()) {
            return sendResult200(BeanMapper.mapList(list, DictionaryDTO.class));
        } else {
            return sendResult200();
        }
    }

    /**
     * 获取体检报告验证码
     */
    @RequestMapping(value = "/findVerificationCode")
    public BaseResult findVerificationCode(@RequestParam("reportOrganization") int reportOrganization) {
        Dictionary dictionary = new Dictionary().selectOne(new EntityWrapper().eq("dictionary_code","VERIFICATION_CODE_"+reportOrganization));
        return dictionary!=null?sendResult200(dictionary.getContent()):sendResult(Constants.RESPONSE_CODE_602,Constants.RESPONSE_MESSAGE_602);
    }


}
